<template>
  <div class="platformBind">
    <h1 class="auto_title" style="margin:10px 0 30px 0">社交平台绑定列表 </h1>
    <SocialBind></SocialBind>
  </div>
</template>

<script>
    import {query, del, setDefault, update, add} from "@/api/mail/admin/mail-set";
    import SocialBind from  '@/components/automation/socialAccount/SocialBind'
    export default {
        name: "platformBind",
        components:{SocialBind},
        data() {
            return {
                dialogVisible: false,
                emailList: [],
                form: {
                    id: '',
                    emailAddr: '',
                    authorizationCode: '',
                    passWord: ''
                },
                rules: {
                    emailAddr: [
                        {required: true, message: '请输入邮箱地址', trigger: 'blur'},
                    ],
                    passWord: [
                        {required: true, message: '请输入邮箱登陆密码', trigger: 'blur'},
                    ]
                }
            }
        },
        mounted() {
            this.getData(1)//发送
        },
        methods: {
            bindNew() {
                this.resetForm();
                this.dialogVisible = true;
            },
            //设为默认邮箱
            setDefault(id) {
                this.$confirm("确定所选邮箱设为默认邮箱吗?", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "success"
                }).then(() => {
                    setDefault(id).then(res => {
                        this.dealEnd(res, 1)
                    }).catch(err => {
                        console.log(err)
                    });
                }).catch(()=>{})
            },
            //编辑
            edit(row) {
                if(this.$refs.ruleForm){
                    this.$refs.ruleForm.resetFields();
                }
                this.form.id = row.id;
                this.form.emailAddr = row.emailAddr;
                this.form.authorizationCode = row.authorizationCode;
                this.form.passWord = row.passWord;
                this.dialogVisible = true;
            },
            //删除
            delRow(id, type) {
                this.$confirm("确定将所选邮箱解除绑定?", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "success"
                }).then(() => {
                    del(id, type).then(res => {
                        this.dealEnd(res, type)
                    }).catch(err => {
                        console.log(err)
                    });
                })
            },
            //绑定邮箱
            submit() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        if (this.form.id) { //编辑更新
                            update(this.form, 1).then(res => {
                                this.dealEnd(res, 1)
                            }).catch(err => {
                                console.log(err)
                            });
                        } else { //新增
                            add(this.form, 1).then(res => {
                                this.dealEnd(res, 1)
                            }).catch(err => {
                                console.log(err)
                            });
                        }
                    } else {
                        return false;
                    }
                });
            },
            dealEnd(res, type) {
                if(res.data.success){
                    this.$store.dispatch('update_emailList')
                    this.dialogVisible = false;
                    this.$message({
                        type: "success",
                        message: res.data.msg
                    });
                    this.form.id = '';
                    if(this.$refs.ruleForm){
                        this.$refs.ruleForm.resetFields();
                    }
                    this.getData(type);
                }else{
                    this.$message({
                        type: "success",
                        message: res.data.msg
                    });
                }
            },
            getData(type) {
                query(type).then(res => {
                    this.emailList = res.data.data;
                }).catch(err => {
                    console.log(err)
                });
            },
            //重置
            resetForm() {
                this.form = {
                    id: '',
                    emailAddr: '',
                    authorizationCode: '',
                    passWord: ''
                }
            }
        }
    }
</script>

<style scoped lang="scss">
  .platformBind {
    background-color: #fff;
    .detail {
      margin-top: 30px;
    }
    .auto_title {
      color: #435B77;
      font-weight: 600;
      padding-left: 2px;
      font-size: 16px;
    }
    .el-dialog__header {
      margin-bottom: 25px;
    }
    .el-row {
      background-color: #6666663b;
      color: #333;
      padding: 8px 15px;
      font-size: 14px;
      border-bottom: 1px solid #0000009e;

      span {
        cursor: pointer;
      }

      .el-col.el-col-4 {
        span {
          margin: 0 15px;
        }
      }
    }

    .head.el-row {
      padding: 15px;
      background-color: #66666687;
      border-bottom: 0;
    }

  }
</style>
